package com.robot.member.controller;

import com.alibaba.fastjson.JSON;
import com.robot.member.client.MerchantClient;
import com.robot.member.pojo.MerchantApply;
import com.robot.member.service.MemberService;
import com.robot.member.service.MerchantApplyService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import util.CheckUtil;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 商户入驻申请merchant_apply控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/merchant_apply")
public class MerchantApplyJsonController {

    @Autowired
    private MerchantApplyService merchantApplyService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CheckUtil checkUtil;

    @Autowired
    private MerchantClient merchantClient;

    /**
     * 条件查找merchant_apply列表并分页
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/third/findMerchantAppliesBySearch/{page}/{size}", produces = "application/json; charset=utf-8")
    public String thirdFindMerchantAppliesBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<MerchantApply> pageList = merchantApplyService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 通过areaId查询merchant_apply列表
     *
     * @param areaId
     * @return
     */
    @PostMapping(value = "/third/findMerchantAppliesByAreaId/{areaId}", produces = "application/json; charset=utf-8")
    public String thirdFindMerchantAppliesByAreaId(@PathVariable String areaId) {
        return JSON.toJSONString(merchantApplyService.findAllByAreaIdAndDeletedAtIsNullOrderByUpdatedAtDesc(areaId));
    }

    /**
     * 通过areaId和status查询merchant_apply列表
     *
     * @param areaId
     * @param status
     * @return
     */
    @PostMapping(value = "/third/findMerchantAppliesByAreaIdAndStatus/{areaId}/{status}", produces = "application/json; charset=utf-8")
    public String thirdFindMerchantAppliesByAreaIdAndStatus(@PathVariable String areaId, @PathVariable String status) {
        return JSON.toJSONString(merchantApplyService.findAllByAreaIdAndStatusAndDeletedAtIsNullOrderByUpdatedAtDesc(areaId, status));
    }

    /**
     * 通过id查询merchant_apply详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/third/findMerchantApplyById/{id}", produces = "application/json; charset=utf-8")
    public String thirdFindMerchantApplyById(@PathVariable String id) {
        return JSON.toJSONString(merchantApplyService.findById(id));
    }

    /**
     * 通过storeId查询merchant_apply详情
     *
     * @param storeId
     * @return
     */
    @GetMapping(value = "/third/findMerchantApplyByStoreId/{storeId}", produces = "application/json; charset=utf-8")
    public String thirdFindTopByStoreIdOrderByCreatedAtDesc(@PathVariable String storeId) {
        return JSON.toJSONString(merchantApplyService.findTopByStoreIdOrderByUpdatedAtDesc(storeId));

    }

    /**
     * 通过id修改merchant_apply详情
     *
     * @param apply
     * @return
     */
    @RequestMapping(value = "/third/updateMerchantApply", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    @Transactional(rollbackFor = Exception.class)
    public boolean thirdUpdateMerchantApply(@RequestBody String apply) {
        merchantApplyService.update(JSON.parseObject(apply, MerchantApply.class));
        return true;
    }
}
